home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / objects in c ƒ / OIC Sources / String.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-05  |  2.3 KB  |  145 lines  |  [TEXT/KAHL]

  1. /*    
  2.  *        String abstract class
  3.  *
  4.  *            Copyright © John Wainwright 1988
  5.  *
  6.  *    Superclasses:
  7.  *    
  8.  *    Purpose:
  9.  *
  10.  *    methods:
  11.  */
  12.  
  13. #include "oic.h"
  14. #include "generics.h"
  15.  
  16. class       String;
  17.  
  18. struct string_i
  19. {
  20.     char    *s_string;
  21. };
  22. typedef struct string_i string_i;
  23.  
  24. /* ------------------------- Instance methods ---------------------------- */
  25.  
  26. method string  
  27. _new(self, string, val)
  28.     object        self;
  29.     string_i    *string;
  30.     char        **val;
  31. {
  32.     char    *strcpy();
  33.  
  34.     string->s_string = strcpy(salloc(strlen(*val) + 1), *val);
  35.     return self;
  36. }
  37.  
  38. method char *
  39. _stringOf(self, string)
  40.     object        self;
  41.     string_i    *string;
  42. {
  43.     return string->s_string;
  44. }
  45.  
  46. method object
  47. _repList(self)
  48.     object    self;
  49. {
  50.     return copy(self);
  51. }
  52.  
  53. method object
  54. _copy(self, string)
  55.     object        self;
  56.     register string_i    *string;
  57. {
  58.     return New(String, string->s_string);
  59. }
  60.  
  61. method
  62. _print(self, string)
  63.     object        self;
  64.     string_i    *string;
  65. {
  66.     gprintf(screen, "%s\n", string->s_string);
  67. }
  68.  
  69. method
  70. _draw(self, string)
  71.     object        self;
  72.     string_i    *string;
  73. {
  74.     DrawString(CtoPstr(string->s_string));
  75. }
  76.  
  77. method int
  78. _equal(self, string, other)
  79.     object        self;
  80.     string_i    *string;
  81.     object        *other;
  82. {
  83.     return (strcmp(string->s_string, localIVs(*other, string_i)->s_string) == 0);
  84. }
  85.  
  86. method int
  87. _hashOf(self, string)        /* return a +ve int hash value for string    */
  88.     object        self;
  89.     string_i    *string;
  90. {
  91.     register int     hash;
  92.     register char    *s;
  93.     
  94.     for (s = string->s_string; *s; )
  95.         hash += *s++;
  96.     
  97.     return hash & 0x7fff;
  98. }
  99.  
  100. method object
  101. _cantDo(self, string, ap)        /* unknown method    */
  102.     object        self;
  103.     string_i    *string;
  104.     struct
  105.     {
  106.         GenericTable    *gen;
  107.         char            *args;
  108.     } *ap;
  109. {
  110.     gprintf(error, "string \"%s\" can't understand \"%s\"\n", string->s_string, GenericName(ap->gen));
  111.  
  112.     return ApplyGeneric(ap->gen, String, ap->args);
  113. }
  114.  
  115.  
  116. method
  117. _dispose(self, string)
  118.     object        self;
  119.     string_i    *string;
  120. {
  121.     free(string->s_string);
  122.     Super(self);
  123. }
  124.  
  125. /* ------------------- Init the String class ------------------------------- */
  126.  
  127. InitString()
  128. {
  129.     String = NewClass(sizeof(string_i), 0, "String", END);
  130.     AddMethods(String, 
  131.         newGeneric,            _new,
  132.         stringOfGeneric,    _stringOf,
  133.         repListGeneric,        _repList,
  134.         printGeneric,         _print,
  135.         equalGeneric,        _equal,
  136.         hashOfGeneric,        _hashOf,
  137.         copyGeneric,        _copy,
  138.         cantDoGeneric,        _cantDo,
  139.         deepCopyGeneric,    _copy,
  140.         disposeGeneric,        _dispose,
  141.         deepDisposeGeneric,    _dispose,
  142.         END);
  143. }
  144.  
  145.